/**
 * Created by Administrator on 2017/5/1.
 */
var submitButton=document.getElementById('submit');
var messageElement=document.getElementById('message');
var myFormElement=document.getElementById('myForm');
submitButton.onclick=function(e) {
    e.preventDefault();

    var roomIdElement = document.querySelector('#myForm input[name=room_id]');
    var roomId = roomIdElement.value;
    if (roomId == "") {
        messageElement.innerHTML = '房间号不能为空';
        return;
    };

    var roomTypeElement = document.querySelector('#myForm input[name=room_type]');
    var roomType = roomTypeElement.value;
    if (roomType == "") {
        messageElement.innerHTML = '房间类型不能为空';
        return;
    };

    var roomNumElement = document.querySelector('#myForm input[name=room_limit]');
    var roomNum = roomNumElement.value;
    if (roomNum == "") {
        messageElement.innerHTML = '限制人数不能为空';
        return;
    }
    var roomImgElement = document.querySelector('#myForm input[name=image]');
    var roomImg = roomImgElement.value;
    if (roomImg == "") {
        messageElement.innerHTML = '头像不能为空';
        return;
    };
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (this.readyState == 4) {
            if (this.status == 200) {
                var result = JSON.parse(this.responseText);
                if (result.error == 1) {
                    messageElement.innerHTML = result.message;
                } else {
                    messageElement.innerHTML = "创建成功";
                    location.href = 'Index.php';
                }
            } else {

                alert("程序出现异常");
            }
        }
    };
    xhr.open(myFormElement.method, myFormElement.action);
    var data = new FormData();
    data.append('roomId', roomId);
    data.append('roomType', roomType);
    data.append('roomNum', roomNum);
    data.append('image', roomImgElement.files[0]);
    xhr.send(data);
}
